<?php

namespace app\adminapi\controller\v1\marketing\score;

use app\adminapi\controller\AuthController;
use app\Request;
use app\services\activity\score\StoreScoreRuleServices;
use think\facade\App;

class StoreScoreRule extends AuthController
{
    protected $services;

    /**
     * StoreScoreRule constructor.
     * @param App $app
     * @param StoreScoreRuleServices $service
     */
    public function __construct(App $app, StoreScoreRuleServices $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    public function list(Request  $request){
        $list = $this->services->list();
        return app('json')->success($list);
    }

    /**
     * 获取列表
     * @return mixed
     */
    public function index()
    {
        $where = $this->request->getMore([
        ]);
        $data = $this->services->getList($where);
        return app('json')->success($data);
    }

    /**
     * 保存
     * @return mixed
     */
    public function save($id = 0)
    {
        $data = $this->request->postMore([
            ['id', $id],
            ['rule_name',''],//规则名称
            ['spend_price',0.00],//每满多少元
            ['spend_number',0],//每满多少次
            ['product_type',0],//商品条件 1通用 0部分
            ['products',[]],
            ['score',0],//额外奖励积分额
            ['multiple',0],//原有积分倍数
            ['time_type',0],//有效状态 0永久 1有效期
            ['start_time',''],//有效时间开始
            ['end_time',''],//有效时间结束
            ['admin_id',$this->adminId],
            ['admin_name',$this->adminInfo['real_name']],
        ]);
        $this->services->save($data);
        return app('json')->success(100021);
    }

    /**
     * 删除
     * @param int $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) return app('json')->fail(100100);
        $this->services->del($id);
        return app('json')->success(100002);
    }

    /**
     * 读取
     * @return mixed
     */
    public function read($id)
    {
        $data = $this->services->read($id);
        return app('json')->success($data);
    }
}
